home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectDraw / DDEnum / ddenum.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  9.8 KB  |  297 lines

  1. //-----------------------------------------------------------------------------
  2. // File: ddenum.cpp
  3. //
  4. // Desc: This sample demonstrates how to enumerate all of the devices and show
  5. //       the driver information about each.
  6. //
  7. //
  8. // Copyright (c) 1998-2001 Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #define STRICT
  11. #include <windows.h>
  12. #include <ddraw.h>
  13. #include "resource.h"
  14.  
  15.  
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Defines, constants, and global variables
  19. //-----------------------------------------------------------------------------
  20. #define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
  21. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
  22.  
  23. #define MAX_DEVICES     32
  24.  
  25. struct DEVICEIDENT_STRUCT
  26. {
  27.     DDDEVICEIDENTIFIER2 DeviceInfo;
  28.     DDDEVICEIDENTIFIER2 DeviceInfoHost;
  29. };
  30.  
  31. DEVICEIDENT_STRUCT g_DeviceIdent[MAX_DEVICES];
  32. int                g_iMaxDevices = 0;
  33.  
  34.  
  35.  
  36.  
  37. //-----------------------------------------------------------------------------
  38. // Name: UpdateInfoDlgText()
  39. // Desc: Update all of the text and buttons in the dialog
  40. //-----------------------------------------------------------------------------
  41. void UpdateInfoDlgText( HWND hDlg, int iCurrent, DWORD dwHost ) 
  42. {
  43.     TCHAR                 strBuffer[128];
  44.     GUID*                 pGUID;
  45.     LPDDDEVICEIDENTIFIER2 pDI;
  46.  
  47.     if( dwHost == DDGDI_GETHOSTIDENTIFIER )
  48.         CheckRadioButton( hDlg, IDC_RADIO_DEVICE, IDC_RADIO_HOST, IDC_RADIO_HOST );
  49.     else
  50.         CheckRadioButton( hDlg, IDC_RADIO_DEVICE, IDC_RADIO_DEVICE, IDC_RADIO_DEVICE );
  51.  
  52.     pDI = &g_DeviceIdent[iCurrent].DeviceInfo;
  53.     if( dwHost == DDGDI_GETHOSTIDENTIFIER )
  54.         pDI = &g_DeviceIdent[iCurrent].DeviceInfoHost;
  55.  
  56.     wsprintf( strBuffer, "Device information for device %d of %d", 
  57.               iCurrent + 1, g_iMaxDevices );
  58.     SetDlgItemText( hDlg, IDC_RADIO_DEVICE, strBuffer );
  59.  
  60.     // Device ID stuff:
  61.     wsprintf( strBuffer,"%08X",pDI->dwVendorId );
  62.     SetDlgItemText( hDlg, IDC_DWVENDORID, strBuffer );
  63.  
  64.     wsprintf( strBuffer,"%08X",pDI->dwDeviceId );
  65.     SetDlgItemText( hDlg, IDC_DWDEVICEID, strBuffer );
  66.  
  67.     wsprintf( strBuffer,"%08X",pDI->dwSubSysId );
  68.     SetDlgItemText( hDlg, IDC_DWSUBSYS, strBuffer );
  69.  
  70.     wsprintf( strBuffer,"%08X",pDI->dwRevision );
  71.     SetDlgItemText( hDlg, IDC_DWREVISION, strBuffer );
  72.  
  73.     // Driver version:
  74.     wsprintf( strBuffer, "%d.%02d.%02d.%04d",  
  75.               HIWORD( pDI->liDriverVersion.u.HighPart ),
  76.               LOWORD( pDI->liDriverVersion.u.HighPart ),
  77.               HIWORD( pDI->liDriverVersion.u.LowPart  ),
  78.               LOWORD( pDI->liDriverVersion.u.LowPart  ) );
  79.     SetDlgItemText( hDlg, IDC_VERSION, strBuffer );
  80.  
  81.     // Device description and HAL filename
  82.     SetDlgItemText( hDlg, IDC_DESCRIPTION, pDI->szDescription );
  83.     SetDlgItemText( hDlg, IDC_FILENAME,    pDI->szDriver );
  84.  
  85.     // Unique driver/device identifier:
  86.     pGUID = &pDI->guidDeviceIdentifier;
  87.     wsprintf( strBuffer, "%08X-%04X-%04X-%02X%02X%02X%02X%02X%02X%02X%02X",
  88.               pGUID->Data1, 
  89.               pGUID->Data2, 
  90.               pGUID->Data3,
  91.               pGUID->Data4[0], pGUID->Data4[1], pGUID->Data4[2], pGUID->Data4[3],
  92.               pGUID->Data4[4], pGUID->Data4[5], pGUID->Data4[6], pGUID->Data4[7] );
  93.     SetDlgItemText( hDlg, IDC_GUID, strBuffer );
  94.  
  95.     // WHQL Level
  96.     wsprintf( strBuffer,"%08x", pDI->dwWHQLLevel );
  97.     SetDlgItemText( hDlg, IDC_STATIC_WHQLLEVEL, strBuffer );
  98.  
  99.     // Change the state and style of the Prev and Next buttons if needed
  100.     HWND hNext = GetDlgItem( hDlg, IDC_NEXT );
  101.     HWND hPrev = GetDlgItem( hDlg, IDC_PREV );
  102.  
  103.     if( 0 == iCurrent )
  104.     {
  105.         // The Prev button should be disabled
  106.         SetFocus( GetDlgItem( hDlg, IDOK ) );
  107.         SendDlgItemMessage( hDlg, IDC_PREV, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
  108.         SendMessage( hDlg, DM_SETDEFID, IDOK, 0 );
  109.         EnableWindow( hPrev, FALSE );
  110.     }
  111.     else
  112.     {
  113.         if( IsWindowEnabled( hPrev ) == FALSE )
  114.             EnableWindow( hPrev, TRUE );
  115.     }
  116.  
  117.     if( iCurrent >= (g_iMaxDevices - 1) )
  118.     {
  119.         // The Next button should be disabled
  120.         SetFocus( GetDlgItem( hDlg, IDOK ) );
  121.         SendDlgItemMessage( hDlg, IDC_NEXT, BM_SETSTYLE, BS_PUSHBUTTON, TRUE );
  122.         SendMessage(hDlg, DM_SETDEFID, IDOK, 0 );
  123.         EnableWindow( hNext, FALSE );
  124.     }
  125.     else
  126.     {
  127.         if( IsWindowEnabled( hNext ) == FALSE)
  128.             EnableWindow( hNext, TRUE );
  129.     }
  130. }
  131.  
  132.  
  133.  
  134.  
  135. //-----------------------------------------------------------------------------
  136. // Name: InfoDlgProc()
  137. // Desc: The dialog window proc
  138. //-----------------------------------------------------------------------------
  139. BOOL CALLBACK InfoDlgProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
  140. {
  141.     static int   iCurrent = 0;
  142.     static DWORD dwHost   = 0;
  143.  
  144.     switch (message)
  145.     {
  146.         case WM_INITDIALOG:
  147.             // Setup the first devices text
  148.             UpdateInfoDlgText( hDlg, iCurrent, dwHost );
  149.             break;
  150.  
  151.         case WM_COMMAND:
  152.             switch (LOWORD(wParam))
  153.             {
  154.                 case IDOK:
  155.                 case IDCANCEL:
  156.                     EndDialog( hDlg, TRUE );
  157.                     break;
  158.  
  159.                 case IDC_PREV:
  160.                     // Show the previous device
  161.                     if( iCurrent )
  162.                         iCurrent--;
  163.  
  164.                     UpdateInfoDlgText( hDlg, iCurrent, dwHost );
  165.                     break;
  166.  
  167.                 case IDC_NEXT:
  168.                     // Show the next device
  169.                     if( iCurrent < g_iMaxDevices )
  170.                         iCurrent++;
  171.  
  172.                     UpdateInfoDlgText( hDlg, iCurrent, dwHost );
  173.                     break;
  174.  
  175.                 case IDC_RADIO_HOST:
  176.                     dwHost = DDGDI_GETHOSTIDENTIFIER;
  177.  
  178.                     UpdateInfoDlgText( hDlg, iCurrent, dwHost );
  179.                     break;
  180.  
  181.                 case IDC_RADIO_DEVICE:
  182.                     dwHost = 0;
  183.  
  184.                     UpdateInfoDlgText( hDlg, iCurrent, dwHost );
  185.                     break;
  186.  
  187.                 default:
  188.                     return FALSE; // Message not handled 
  189.             }
  190.  
  191.         default:
  192.             return FALSE; // Message not handled 
  193.     }
  194.  
  195.     return TRUE; // Message handled 
  196. }
  197.  
  198.  
  199.  
  200.  
  201. //-----------------------------------------------------------------------------
  202. // Name: DDEnumCallbackEx()
  203. // Desc: This callback gets the information for each device enumerated
  204. //-----------------------------------------------------------------------------
  205. BOOL WINAPI DDEnumCallbackEx( GUID *pGUID, LPSTR pDescription, LPSTR strName,
  206.                               LPVOID pContext, HMONITOR hm )
  207. {
  208.     LPDIRECTDRAW7 pDD = NULL;    
  209.     HRESULT hr;
  210.  
  211.     // Create a DirectDraw object using the enumerated GUID
  212.     if( FAILED( hr = DirectDrawCreateEx( pGUID, (VOID**)&pDD, 
  213.                                          IID_IDirectDraw7, NULL ) ) )
  214.         return DDENUMRET_CANCEL;
  215.  
  216.     // Get the device information and save it
  217.     pDD->GetDeviceIdentifier( &g_DeviceIdent[g_iMaxDevices].DeviceInfo, 0 );
  218.     pDD->GetDeviceIdentifier( &g_DeviceIdent[g_iMaxDevices].DeviceInfoHost, 
  219.                               DDGDI_GETHOSTIDENTIFIER );
  220.  
  221.     // Finished with the DirectDraw object, so release it
  222.     SAFE_RELEASE( pDD ); 
  223.  
  224.     // Bump to the next open slot or finish the callbacks if full
  225.     if( g_iMaxDevices < MAX_DEVICES )
  226.         g_iMaxDevices++;
  227.     else
  228.         return DDENUMRET_CANCEL;
  229.  
  230.     return DDENUMRET_OK;
  231. }
  232.  
  233.  
  234.  
  235.  
  236. //-----------------------------------------------------------------------------
  237. // Name: DDEnumCallback()
  238. // Desc: Old style callback retained for backwards compatibility
  239. //-----------------------------------------------------------------------------
  240. BOOL WINAPI DDEnumCallback( GUID *pGUID, LPSTR pDescription, 
  241.                             LPSTR strName, LPVOID pContext )
  242. {
  243.     return ( DDEnumCallbackEx( pGUID, pDescription, strName, pContext, NULL ) );
  244. }
  245.  
  246.  
  247.  
  248.  
  249. //-----------------------------------------------------------------------------
  250. // Name: WinMain()
  251. // Desc: Entry point to the program. Initializes everything and calls
  252. //       DirectDrawEnumerateEx() to get all of the device info.
  253. //-----------------------------------------------------------------------------
  254. int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, int nCmdShow )
  255. {
  256.     LPDIRECTDRAWENUMERATEEX pDirectDrawEnumerateEx;
  257.     HINSTANCE               hDDrawDLL = NULL;
  258.  
  259.     // Do a GetModuleHandle and GetProcAddress in order to get the
  260.     // DirectDrawEnumerateEx function
  261.     hDDrawDLL = GetModuleHandle("DDRAW");
  262.     if( NULL == hDDrawDLL )
  263.     {
  264.         MessageBox( NULL, "LoadLibrary() FAILED", 
  265.                     "DirectDraw Sample", MB_OK | MB_ICONERROR );
  266.         return -1;
  267.     }
  268.  
  269.     pDirectDrawEnumerateEx = (LPDIRECTDRAWENUMERATEEX) GetProcAddress( hDDrawDLL, "DirectDrawEnumerateExA" );
  270.     if( pDirectDrawEnumerateEx )
  271.     {
  272.         pDirectDrawEnumerateEx( DDEnumCallbackEx, NULL,
  273.                                 DDENUM_ATTACHEDSECONDARYDEVICES |
  274.                                 DDENUM_DETACHEDSECONDARYDEVICES |
  275.                                 DDENUM_NONDISPLAYDEVICES );
  276.     }
  277.     else
  278.     {
  279.         // Old DirectDraw, so do it the old way
  280.         DirectDrawEnumerate( DDEnumCallback, NULL );
  281.     }
  282.  
  283.     if( 0 == g_iMaxDevices )
  284.     {
  285.         MessageBox( NULL, "No devices to enumerate.", 
  286.                     "DirectDraw Sample", MB_OK | MB_ICONERROR );
  287.         return -1;
  288.     }
  289.  
  290.     // Bring up the dialog to show all the devices
  291.     DialogBox( hInst, MAKEINTRESOURCE(IDD_DRIVERINFO), 
  292.                GetDesktopWindow(), (DLGPROC)InfoDlgProc );
  293.  
  294.     return 0;
  295. }
  296.  
  297.